{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "78ee8d4a",
   "metadata": {},
   "source": [
    "根据移动平均值提供策略\n",
    "- MA5>=MA10>=MA20>=MA30\n",
    "- 近7日的close与MA5对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "b092929c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import webbrowser\n",
    "from datetime import datetime, timedelta\n",
    "import tushare as ts\n",
    "import numpy as np\n",
    "\n",
    "ts_token = \"a739317098dd53b37c489d35c135e84524407f1cf5bc339d55f35bdd\"\n",
    "ts.set_token(ts_token)\n",
    "pro = ts.pro_api()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5385ebb0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8f47636a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_stock(code):\n",
    "    # 获取股票数据\n",
    "    end_date = datetime.now().strftime(\"%Y%m%d\")\n",
    "    start_date = datetime.strftime(datetime.now() - timedelta(365), \"%Y%m%d\")\n",
    "    df = pro.daily(ts_code=code).sort_values(by=\"trade_date\")\n",
    "    df[\"MA5\"]= df[\"close\"].rolling(4).mean()\n",
    "    df[\"MA10\"]= df[\"close\"].rolling(9).mean()\n",
    "    df[\"MA20\"]= df[\"close\"].rolling(19).mean()\n",
    "    df[\"MA30\"]= df[\"close\"].rolling(29).mean()\n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "08e139c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = get_stock(\"000001.SZ\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e40d4530",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(6000, 15)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a5a8521d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>trade_date</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>change</th>\n",
       "      <th>pct_chg</th>\n",
       "      <th>vol</th>\n",
       "      <th>amount</th>\n",
       "      <th>MA5</th>\n",
       "      <th>MA10</th>\n",
       "      <th>MA20</th>\n",
       "      <th>MA30</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20230606</td>\n",
       "      <td>11.91</td>\n",
       "      <td>12.09</td>\n",
       "      <td>11.82</td>\n",
       "      <td>11.84</td>\n",
       "      <td>11.91</td>\n",
       "      <td>-0.07</td>\n",
       "      <td>-0.5877</td>\n",
       "      <td>833273.47</td>\n",
       "      <td>997039.312</td>\n",
       "      <td>11.8175</td>\n",
       "      <td>11.861111</td>\n",
       "      <td>12.188947</td>\n",
       "      <td>12.340000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20230607</td>\n",
       "      <td>11.89</td>\n",
       "      <td>12.07</td>\n",
       "      <td>11.88</td>\n",
       "      <td>11.94</td>\n",
       "      <td>11.84</td>\n",
       "      <td>0.10</td>\n",
       "      <td>0.8446</td>\n",
       "      <td>643697.84</td>\n",
       "      <td>770386.878</td>\n",
       "      <td>11.9050</td>\n",
       "      <td>11.862222</td>\n",
       "      <td>12.141053</td>\n",
       "      <td>12.334483</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20230608</td>\n",
       "      <td>11.96</td>\n",
       "      <td>12.20</td>\n",
       "      <td>11.86</td>\n",
       "      <td>12.12</td>\n",
       "      <td>11.94</td>\n",
       "      <td>0.18</td>\n",
       "      <td>1.5075</td>\n",
       "      <td>869397.12</td>\n",
       "      <td>1048266.236</td>\n",
       "      <td>11.9525</td>\n",
       "      <td>11.864444</td>\n",
       "      <td>12.114737</td>\n",
       "      <td>12.328966</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20230609</td>\n",
       "      <td>12.06</td>\n",
       "      <td>12.09</td>\n",
       "      <td>11.85</td>\n",
       "      <td>11.88</td>\n",
       "      <td>12.12</td>\n",
       "      <td>-0.24</td>\n",
       "      <td>-1.9802</td>\n",
       "      <td>1315284.76</td>\n",
       "      <td>1566929.274</td>\n",
       "      <td>11.9450</td>\n",
       "      <td>11.853333</td>\n",
       "      <td>12.064737</td>\n",
       "      <td>12.320690</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20230612</td>\n",
       "      <td>11.82</td>\n",
       "      <td>11.90</td>\n",
       "      <td>11.76</td>\n",
       "      <td>11.79</td>\n",
       "      <td>11.88</td>\n",
       "      <td>-0.09</td>\n",
       "      <td>-0.7576</td>\n",
       "      <td>758747.03</td>\n",
       "      <td>896015.709</td>\n",
       "      <td>11.9325</td>\n",
       "      <td>11.844444</td>\n",
       "      <td>12.021053</td>\n",
       "      <td>12.304483</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     ts_code trade_date   open   high    low  close  pre_close  change  \\\n",
       "4  000001.SZ   20230606  11.91  12.09  11.82  11.84      11.91   -0.07   \n",
       "3  000001.SZ   20230607  11.89  12.07  11.88  11.94      11.84    0.10   \n",
       "2  000001.SZ   20230608  11.96  12.20  11.86  12.12      11.94    0.18   \n",
       "1  000001.SZ   20230609  12.06  12.09  11.85  11.88      12.12   -0.24   \n",
       "0  000001.SZ   20230612  11.82  11.90  11.76  11.79      11.88   -0.09   \n",
       "\n",
       "   pct_chg         vol       amount      MA5       MA10       MA20       MA30  \n",
       "4  -0.5877   833273.47   997039.312  11.8175  11.861111  12.188947  12.340000  \n",
       "3   0.8446   643697.84   770386.878  11.9050  11.862222  12.141053  12.334483  \n",
       "2   1.5075   869397.12  1048266.236  11.9525  11.864444  12.114737  12.328966  \n",
       "1  -1.9802  1315284.76  1566929.274  11.9450  11.853333  12.064737  12.320690  \n",
       "0  -0.7576   758747.03   896015.709  11.9325  11.844444  12.021053  12.304483  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "57a77830",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 如果MA5>=MA10>=MA20>=MA30标记为1\n",
    "df[\"ma\"] = df[[\"MA5\",\"MA10\",\"MA20\",\"MA30\"]].apply(lambda x:1 if x[0]>=x[1]>=x[2]>=x[3] else 0,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f24cc374",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>trade_date</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>change</th>\n",
       "      <th>pct_chg</th>\n",
       "      <th>vol</th>\n",
       "      <th>amount</th>\n",
       "      <th>MA5</th>\n",
       "      <th>MA10</th>\n",
       "      <th>MA20</th>\n",
       "      <th>MA30</th>\n",
       "      <th>ma</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20230606</td>\n",
       "      <td>11.91</td>\n",
       "      <td>12.09</td>\n",
       "      <td>11.82</td>\n",
       "      <td>11.84</td>\n",
       "      <td>11.91</td>\n",
       "      <td>-0.07</td>\n",
       "      <td>-0.5877</td>\n",
       "      <td>833273.47</td>\n",
       "      <td>997039.312</td>\n",
       "      <td>11.8175</td>\n",
       "      <td>11.861111</td>\n",
       "      <td>12.188947</td>\n",
       "      <td>12.340000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20230607</td>\n",
       "      <td>11.89</td>\n",
       "      <td>12.07</td>\n",
       "      <td>11.88</td>\n",
       "      <td>11.94</td>\n",
       "      <td>11.84</td>\n",
       "      <td>0.10</td>\n",
       "      <td>0.8446</td>\n",
       "      <td>643697.84</td>\n",
       "      <td>770386.878</td>\n",
       "      <td>11.9050</td>\n",
       "      <td>11.862222</td>\n",
       "      <td>12.141053</td>\n",
       "      <td>12.334483</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20230608</td>\n",
       "      <td>11.96</td>\n",
       "      <td>12.20</td>\n",
       "      <td>11.86</td>\n",
       "      <td>12.12</td>\n",
       "      <td>11.94</td>\n",
       "      <td>0.18</td>\n",
       "      <td>1.5075</td>\n",
       "      <td>869397.12</td>\n",
       "      <td>1048266.236</td>\n",
       "      <td>11.9525</td>\n",
       "      <td>11.864444</td>\n",
       "      <td>12.114737</td>\n",
       "      <td>12.328966</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20230609</td>\n",
       "      <td>12.06</td>\n",
       "      <td>12.09</td>\n",
       "      <td>11.85</td>\n",
       "      <td>11.88</td>\n",
       "      <td>12.12</td>\n",
       "      <td>-0.24</td>\n",
       "      <td>-1.9802</td>\n",
       "      <td>1315284.76</td>\n",
       "      <td>1566929.274</td>\n",
       "      <td>11.9450</td>\n",
       "      <td>11.853333</td>\n",
       "      <td>12.064737</td>\n",
       "      <td>12.320690</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20230612</td>\n",
       "      <td>11.82</td>\n",
       "      <td>11.90</td>\n",
       "      <td>11.76</td>\n",
       "      <td>11.79</td>\n",
       "      <td>11.88</td>\n",
       "      <td>-0.09</td>\n",
       "      <td>-0.7576</td>\n",
       "      <td>758747.03</td>\n",
       "      <td>896015.709</td>\n",
       "      <td>11.9325</td>\n",
       "      <td>11.844444</td>\n",
       "      <td>12.021053</td>\n",
       "      <td>12.304483</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     ts_code trade_date   open   high    low  close  pre_close  change  \\\n",
       "4  000001.SZ   20230606  11.91  12.09  11.82  11.84      11.91   -0.07   \n",
       "3  000001.SZ   20230607  11.89  12.07  11.88  11.94      11.84    0.10   \n",
       "2  000001.SZ   20230608  11.96  12.20  11.86  12.12      11.94    0.18   \n",
       "1  000001.SZ   20230609  12.06  12.09  11.85  11.88      12.12   -0.24   \n",
       "0  000001.SZ   20230612  11.82  11.90  11.76  11.79      11.88   -0.09   \n",
       "\n",
       "   pct_chg         vol       amount      MA5       MA10       MA20       MA30  \\\n",
       "4  -0.5877   833273.47   997039.312  11.8175  11.861111  12.188947  12.340000   \n",
       "3   0.8446   643697.84   770386.878  11.9050  11.862222  12.141053  12.334483   \n",
       "2   1.5075   869397.12  1048266.236  11.9525  11.864444  12.114737  12.328966   \n",
       "1  -1.9802  1315284.76  1566929.274  11.9450  11.853333  12.064737  12.320690   \n",
       "0  -0.7576   758747.03   896015.709  11.9325  11.844444  12.021053  12.304483   \n",
       "\n",
       "   ma  \n",
       "4   0  \n",
       "3   0  \n",
       "2   0  \n",
       "1   0  \n",
       "0   0  "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "70b23e07",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    4750\n",
       "1    1250\n",
       "Name: ma, dtype: int64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"ma\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "b69f7d6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "ma_result = []\n",
    "s=0\n",
    "for t,i in df[[\"trade_date\",\"ma\"]].values.tolist():\n",
    "    s+=i\n",
    "    if i==0 and s!=0:\n",
    "        ma_result.append({\n",
    "            \"trade_date\":t,\n",
    "            \"frequency\":s\n",
    "        })\n",
    "        s=0\n",
    "tf_df = pd.DataFrame(ma_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "e7c350e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# df[df[\"trade_date\"].isin([\"20230509\",\"20230510\",\"20230511\",\"20230512\",\"20230513\",\"20230514\",\"20230515\",\"20230516\",\"20230517\",\"20230518\"])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "006be31d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    182.000000\n",
       "mean       6.868132\n",
       "std        5.321091\n",
       "min        1.000000\n",
       "25%        3.000000\n",
       "50%        5.500000\n",
       "75%        9.750000\n",
       "max       26.000000\n",
       "Name: frequency, dtype: float64"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf_df[\"frequency\"].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "fe95fb80",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7747252747252747"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf_df[tf_df[\"frequency\"]>=3].shape[0]/tf_df.shape[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06507bb4",
   "metadata": {},
   "source": [
    "平安银行，出现MA5>=MA10>=MA20>=MA30状态时，连续保持这种状态的均值是6.8天，最大连续保持天数26天"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "d65c4c36",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6.868131868131868"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf_df[\"frequency\"].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "55ce2aa5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ma_array.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b4ba237b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "3120fd30",
   "metadata": {},
   "outputs": [],
   "source": [
    "test = [0,0,1,1,0,1,0,1,1,1,1,0,0,0,1,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "5992572c",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = []\n",
    "s=0\n",
    "for i in test:\n",
    "    s+=i\n",
    "    if i==0 and s!=0:\n",
    "        result.append(s)\n",
    "        s=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "62b1d11a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 1, 4, 1]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e992d89e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5e544f7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "604889b2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c1406cfe",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e659257",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "238088c9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b6475ab5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
